#include "question.h"
#include "database.h"
#include <QSqlQuery>
#include <QSqlError>

question::question(int id,const QString &text,const QString &options)
{
    this->id = id;
    this->text = text;
    this->option = options;

}

bool question::addQuestion(const question &question) {
    QSqlQuery query(Database::getInstance().getDatabase());
    query.prepare("INSERT INTO questions (text, options) VALUES (?, ?)");
    query.addBindValue(question.text);
    query.addBindValue(question.option);


    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Error: " << query.lastError().text();
        return false;
    }
}

bool question::updateQuestion(const question &question) {
    QSqlQuery query(Database::getInstance().getDatabase());
    query.prepare("UPDATE questions SET text = ?, options = ? WHERE id = ?");
    query.addBindValue(question.text);
    query.addBindValue(question.option);
    query.addBindValue(question.id);

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Error: " << query.lastError().text();
        return false;
    }
}

bool question::deleteQuestion(int id) {
    QSqlQuery query(Database::getInstance().getDatabase());
    query.prepare("DELETE FROM questions WHERE id = ?");
    query.addBindValue(id);

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Error: " << query.lastError().text();
        return false;
    }
}
